' DOSDoors - Variable Routines
#Print Vars.bas

#include once "main.bi"
#include once "vars.bi"
#include once "interp.bi"

TotalVariables = -1

Sub AddVariable(VarName As String, VarType As String, VarValue As String)
    #ifdef DEBUG_LOG
        LogString("AddVariable")
    #endif
    
    VarType = Trim(LCase(VarType))

    TotalVariables = TotalVariables + 1

    ReDim Preserve Var_Name(TotalVariables)
    ReDim Preserve Var_Type(TotalVariables)
    ReDim Preserve Var_Val(TotalVariables)

    Var_Name(TotalVariables) = LCase(VarName)
    Var_Type(TotalVariables) = VarType
    Var_Val(TotalVariables) = VarValue
    'Print "Added Variable (" & TotalVariables & "): " & Var_Name(TotalVariables) & " " & Var_Type(TotalVariables) & " " & Var_Val(TotalVariables)

    #ifdef DEBUG_LOG
        LogString("AddVariable:End")
    #endif
End Sub

Function SetVariable(VarName As String, VarValue As String) As Integer
    #ifdef DEBUG_LOG
        LogString("SetVariable")
    #endif

    Dim As Integer tempLoop

    VarName = LCase(VarName)
    Do
        If (tempLoop > TotalVariables) Then
            SetVariable = 0
            Exit Do
        End If

        'If (LCase(Var_Name(tempLoop)) = VarName) Then
        If (Var_Name(tempLoop) = VarName) Then
            Var_Val(tempLoop) = VarValue
            SetVariable = 1
            Exit Do
        End If
        tempLoop = tempLoop + 1
    Loop

    #ifdef DEBUG_LOG
        LogString("SetVariable:End")
    #endif
End Function

Function GetVariable(VarName As String, VarType As Integer = 0) As String
    #ifdef DEBUG_LOG
        LogString("GetVariable")
    #endif

    Dim As Integer tempLoop

    VarName = LCase(VarName)
    Do
        If (tempLoop > TotalVariables) Then
            GetVariable = ""
            Exit Do
        End If

        'If (LCase(Var_Name(tempLoop)) = VarName) Then
        If (Var_Name(tempLoop) = VarName) Then
            If VarType = 1 Then
                GetVariable = LCase(Var_Type(tempLoop)) & "=" & Var_Val(tempLoop)
            Else
                GetVariable = Var_Val(tempLoop)
            End If
            'Print "(" & tempLoop & "/" & TotalVariables & ")'" & VarName & "'="
            Exit Do
        End If

        tempLoop = tempLoop + 1
    Loop

    #ifdef DEBUG_LOG
        LogString("GetVariable:End")
    #endif
End Function